package xin.karry.example.quartz.service;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * 将产生的定时任务job注入到spring容器中，解决spring管理的bean的注入问题，应对在启动定时任务时报空指针异常的情况
 * 该类中的内容可以注释掉，也可以实现spring管理的bean的注入
 */
@Component("springQuartzFactoryBean")
public class SpringQuartzFactoryBean extends SpringBeanJobFactory {
    @Autowired
    private AutowireCapableBeanFactory beanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object obj = super.createJobInstance(bundle);
        beanFactory.autowireBean(obj);
        return obj;
    }
}
